FX_FRAMES = [
    ['FX1.plist','FX1.png'],
];


var FXManager = {

    // 播放视频的默认时间间隔 单位:秒
    VIDEO_DELAY : 0.06,

    init : function () {
        this.loadSpriteFrames();
    },

    // 帧动画相关
    loadSpriteFrames : function () {
        for(var i in FX_FRAMES) {
            cc.SpriteFrameCache.getInstance().addSpriteFrames( FX_FRAMES[i][0], FX_FRAMES[i][1] );
        }
    },

    releaseSpriteFrames : function () {
        cc.SpriteFrameCache.getInstance().removeUnusedSpriteFrames();
    },


    // 创建帧动画特效
    createFXAnimation : function (name, delay) {
        var frames = [];
        var i = 1;
        var f = null;
        var cache = cc.SpriteFrameCache.getInstance()

        while ( cache.getSpriteFrame( this._fxName(name, i) ) ) {
            f = cache.getSpriteFrame( this._fxName(name, i) );
            frames.push(f);
            i += 1;
        }

        if(frames.length > 0) {
            var animation = cc.Animation.create(frames, delay);
            return animation;
        }
        cc.log('FXManager>Animation:no this:'+name);
        return null;
    },

    createFXAction : function (name, delay, isLoop, onOverFunc, target) {
        var animation  = this.createFXAnimation(name, delay);
        if (animation) {
            var action = cc.Animate.create(animation); // 动画
            if (isLoop) {
                action = cc.RepeatForever.create(action);    // 循环
            } else {
                if(onOverFunc) {
                    var overFunc = cc.CallFunc.create(onOverFunc, target);
                    action = cc.Sequence.create(action, overFunc)// 单次结束回调                    
                }
            }
            return action;
        }
        return null;
    },

    createFXAnimationSprite : function (name, delay, isLoop, onOverFunc, target) {
        var action  = this.createFXAction(name, delay, isLoop, onOverFunc, target);
        if (action) {
            var sp = cc.Sprite.create();
            sp.runAction(action);
            return sp
        }
        return null;
    },

    // 命名规则 : 特效名字_帧号 从1开始 
    _fxName : function (name, number) {
        if (number > 999) {
            cc.log("ERROR : FXManager._fxName:number > 999");
            return null;
        } else {
            return name + '_' + glb.formatNumber('00', number) + '.png';
        }
    },

    // 在parent上x,y位置播放特效,播放完自动清理
    playAnimation : function (name, parent, x, y, delay, isLoop, callback) {

    	var _onOver = function () {
    		parent.removeChild(fx);
            if(callback) { callback(name); }
    	}

    	var fx = this.createFXAnimationSprite(name, delay, isLoop, _onOver, null);
    	fx.setPosition(x, y);
    	parent.addChild(fx);
    },

    // 粒子系统
    crateFXParticleSystem : function (plist) {
        var p = cc.ParticleSystem.create(plist);
        return p;
    }, 


    // 发射一个粒子特效
    launchParticleSystem : function (plist, parent, time, fromPosition, toPosition) {
        var p = cc.ParticleSystem.create(plist);
        p.setScale(0.5);
        p.setPosition(fromPosition);
        parent.addChild(p);

        var _func = function (sender) {
            // 结束时删除特效
            parent.removeChild(p);
        }

        this.moveFX(p, time, toPosition, _func);
    },


    // 移动
    moveFX : function (fx, time, position, callback) {
        var move = cc.EaseSineIn.create( cc.MoveTo.create(time, position) ); // 平滑移动
        var overFunc = cc.CallFunc.create(callback, this);      // 结束回调
        fx.runAction(cc.Sequence.create(move, overFunc));
    },
    
    
    // 播放一个例子系统
    playParticleSystem : function(plist, parent, time, position) {
        this.launchParticleSystem(plist, parent, time, position, position);
    },
    
    
    // 关键帧视频
    createVideoSprite : function (name, onOverFunc, target) {

        // 读取关键帧,生成animation
        var i = 0;
        var animation = cc.Animation.create();
        animation.setDelayPerUnit(this.VIDEO_DELAY); //* 视频动画帧间隔,这里暂时固定
        while ( cc.FileUtils.getInstance().isFileExist( this._videoName(name, i) ) ) {
            animation.addSpriteFrameWithFile( this._videoName(name, i) );
            i += 1;
        }
        
        if(i <= 0) {
            cc.log('FXManager>createVideoSprite: No Video: ' + name);
            return null;
        }

        // animation生成action
        var action = cc.Animate.create(animation); 
        if(onOverFunc) {
            var overFunc = cc.CallFunc.create(onOverFunc, target);
            action = cc.Sequence.create(action, overFunc)// 单次结束回调                    
        }

        // 生成Sprite用来播放动画
        var sp = cc.Sprite.create();
        sp.runAction(action);
        return sp
    },

        // 命名规则 : 视频名字_帧号 从0开始
    _videoName : function (name, number) {
        return name + '_' + glb.formatNumber('0000', number) + '.png';
    },

    // 在parent上x,y位置播放视频,播放完自动清理
    playVideo : function (name, parent, x, y, callback) {

    	var _onOver = function () {
    		parent.removeChild(video);
            if(callback) { callback(name); }
    	}

    	var video = this.createVideoSprite(name, _onOver, null);
    	video.setPosition(x, y);
    	parent.addChild(video);
    }

};
